from input import Input

from plugin_system import Plugin

import numpy as np

class XYZSnapshot(Input):
    def __init__(self, filename=None):
        self.filename = filename
        self.fl = None
        
    def initialize(self):
        self.fl = open(self.filename)

    def finalize(self):
        self.fl.close()

    def read(self, system):
        num_objects, step = map(int, next(self.fl).split())
        for j in xrange(num_objects):
            cols = next(self.fl).split()
            name = cols[0]
            positions = np.array(map(float, cols[1:4]), np.float64)
            velocities = np.array(map(float, cols[4:7]), np.float64)
            system.positions[name] = positions
            system.velocities[name] = velocities                         

class XYZSnapshotPluginAdapter(Plugin, XYZSnapshot):
    plugin_name = "XYZSnapshotInput"
    parameter_definitions = {"filename" : (str, True, "File from which to read data.")}
    
    def __init__(self, *args, **user_params):
        params = self.configure_parameters(**user_params)
        XYZSnapshot.__init__(self, filename=params["filename"])
        
